﻿using System;
using System.ServiceModel.DomainServices.Client;
using RIBA.Helper;
using RIBA.Web.Services;

namespace RIBA.DomainServices
{
	public class DomainServiceRIBALive: IDomainServiceRIBA
	{
		private readonly DomainServiceRIBA _service;

		public DomainServiceRIBALive()
		{
			_service = new DomainServiceRIBA();
		}

		
		public void GetServerTime(string time, Action<string, Exception> callback)
		{
			_getServerTimeCallback = callback;
			int sleep = int.Parse(time);
			_service.GetServerTime(sleep, GetServerTime_Callback, null);
		}

		private Action<string, Exception> _getServerTimeCallback;

		private void GetServerTime_Callback(InvokeOperation<string> invokeOperation)
		{
			if (_getServerTimeCallback == null)
				return;

			var errMsg = OperationBaseErrorHandler.GetErrMsg(invokeOperation);
			if (errMsg != "")
			{
				_getServerTimeCallback("", new Exception(errMsg));
				return;
			}

			_getServerTimeCallback(invokeOperation.Value, null);
		}


		public void GetLongAnswer(int stringLength, Action<string, Exception> callback)
		{
			_getLongAnswerCallback = callback;
			_service.GetLongAnswer(stringLength, GetLongAnswer_Callback, null);
		}

		private Action<string, Exception> _getLongAnswerCallback;

		public void GetLongAnswer_Callback(InvokeOperation<string> invokeOperation)
		{
			if (_getLongAnswerCallback == null)
				return;

			var errMsg = OperationBaseErrorHandler.GetErrMsg(invokeOperation);
			if (errMsg != "")
			{
				_getLongAnswerCallback("", new Exception(errMsg));
				return;
			}

			_getLongAnswerCallback(invokeOperation.Value, null);
		}
	}
}
